var countdown = 60;
var utils = require('../../../../utils/util.js') 
var p = require('../../../../utils/password.js')
//获取应用实例
const app = getApp()

Page({
    data: {
        getphone:{
          phone: "",
          code: "",
          disabled: "",
          codemsg: "获取验证码"
        },
        imgcode:'',
        phone:"",
        code:'',
        password:'',
        repassword: '',
        imgurl:'',
        imgtoken:'',
        phoneFocus:true,
        codeFocus:false,
        hide: true,
        scroll_distance: '-660rpx',
    },
    onLoad: function (options) {
        this.getImgCode();
        this.setData({
          'phone': wx.getStorageSync("userinfo").phone
        })
    },
    editphone: function (e) {
        this.setData({
            phone: e.detail.value
        })
    },
    editCode: function (e) {
        this.setData({
            code: e.detail.value
        })
    },
    editimgCode:function(e){
        this.setData({
            imgcode: e.detail.value
        })
    },
    editpassword: function (e) {
      this.setData({
        password: e.detail.value
      })
    },
    editrepassword: function (e) {
      this.setData({
        repassword: e.detail.value
      })
    },
    //获取图形验证码
    getImgCode:function(e){
        var that = this;
        var data = {
            url:"/zfcom/comcodeimg/getcode"
        };
        utils.request(data).then(function(res){
            if (res.code == 0) {
                that.setData({
                    imgurl:res.data.url
                });
                that.setData({
                    imgtoken:res.data.img_token
                });
            }else{
                wx.showToast({
                    title: '获取图形验证码失败，请重试',
                    icon: 'none',
                    duration: 1500
                })
            }
        });
    },
    //获取手机验证码
    getCode:function(e){
        var that = this;
        var phone = this.data.phone;
        var myreg = /^[1][0-9]{10}$/;
        if (phone == "" || !myreg.test(phone)){
            wx.showToast({
                title: '手机号有误！',
                icon: 'none',
                duration: 1500,
            });
            that.setData({
                phoneFocus:true
            });
            that.setData({
                codeFocus:false
            });
            return false;
        }
        if (this.data.getphone.disabled) {
          return false;
        }
        that.setData({
          hide: false,
        });
        that.scroll_distance();
    },
    bindPhone: function () {
        var that = this;
        var code = this.data.code;
        var phone = this.data.phone;
        var password = this.data.password;
        var repassword = this.data.repassword;
        if (!phone) {
          wx.showToast({
            title: '请输入手机号码',
            icon: 'none',
          });
          return false;
        }
        if (!code){
          wx.showToast({
            title: '请输入短信验证码',
            icon: 'none',
          });
          return false;
        }
        if (!password || password.length != 6) {
          wx.showToast({
            title: '请输入6位数的支付密码',
            icon: 'none',
          });
          return false;
        }
        if (!repassword) {
          wx.showToast({
            title: '请输入重复密码',
            icon: 'none',
          });
          return false;
        }
        if (password != repassword) {
          wx.showToast({
            title: '两次密码不一致',
            icon: 'none',
          });
          return false;
        }
        
        var data = {
          url: "/wsgoods/user/editSpwd",
          token: wx.getStorageSync("userinfo").wap_token,
          phone: phone,
          code:code,
          spassword: p.encryption(password)
        };
        utils.request(data).then(function (res) {
            if (res.code == 0) {
                wx.showToast({
                    title: '修改成功',
                    icon: 'none',
                    duration: 1500
                });
                setTimeout(function () {
                  wx.navigateBack({
                    delta: 1
                  })
                }, 1500);
            } else {
                that.getImgCode();
                wx.showToast({
                    title: res.msg,
                    icon: 'none',
                    duration: 1500
                });
            }
        });
    },

    setTime:function(){//验证码
        var that = this;
        if (countdown == 0) {
            var getphone = this.data.getphone;
            getphone.disabled = "";
            getphone.codemsg = "获取验证码";
            this.setData({
                getphone: getphone
            })
            countdown = 60;
            return false;
        } else {
            var getphone = this.data.getphone;
            getphone.disabled = true;
            getphone.codemsg = "重新发送(" + countdown + ")";
            this.setData({
                getphone: getphone,
            })
            countdown--;
        }
        setTimeout(function () {
            that.setTime();
        }, 1000);
    },
    sendCode: function () {
      var that = this;
      var imgcode = this.data.imgcode;
      var imgtoken = this.data.imgtoken;
      var phone = this.data.phone;
      if (imgcode == '') {
        wx.showToast({
          title: '请输入图形验证码！',
          icon: 'none',
          duration: 1500,
        });
        that.setData({
          phoneFocus: false
        });
        that.setData({
          codeFocus: true
        });
        return false;
      }

      //验证图形验证码
        var data = {
            url: "/zfcom/comcodephone/getcode",
            type:"paypwd",
            phone: phone,
            ext:imgcode+','+imgtoken
        };
        utils.request(data).then(function (res) {
            if (res.code == 0) {
                that.close();
                wx.showToast({
                    title: '发送成功',
                    icon: 'none',
                    duration: 1500
                });
                that.setTime();
            } else {
                that.getImgCode();
                wx.showToast({
                    title: res.msg,
                    icon: 'none',
                    duration: 1500
                });
            }
        });
    },
    scroll_distance: function () {
      this.setData({
        scroll_distance: 0
      })
    },
    close: function () {
      this.setData({
        hide: true,
        hide1: true,
        scroll_distance: '-660rpx'
      })
    },
})